package com.tian.arch.config

import android.view.Gravity
import android.widget.Toast
import com.tian.arch.Foundation


/** 全局的Toast工厂 */
typealias ToastFactory = (CharSequence?, Int) -> Toast

class DefaultToastFactory : ToastFactory {
    /**
     * ToastFactory的单例
     */
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { DefaultToastFactory() }
    }

    /**
     * 生成一个toast对象
     */
    override fun invoke(message: CharSequence?, duration: Int): Toast {
        return Toast
            .makeText(Foundation.context(), message, duration)
            .apply {
                setGravity(Gravity.CENTER, 0, 0)
            }
    }
}

/**
 * 提供一个全局toast的方法
 */
fun showToast(msg: String?, duration: Int = Toast.LENGTH_LONG) {
    DefaultToastFactory.instance.invoke(msg, duration).show()
}
